﻿using Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure
{
    public static class SeedData
    {
        public static Guid AdminID = Guid.Parse("08d7ddd8-cd99-4be7-811c-fc599920b206");
        public static void Initialize(IServiceProvider serviceProvider)
        {

            using (var context = new BMSContext(serviceProvider.GetRequiredService<DbContextOptions<BMSContext>>()))
            {
                //种子数据前，先通过Code First生成数据库表
                if (context.User.Any())
                {
                    return;
                }

                Guid departmentID = Guid.NewGuid();
                Guid menuID = Guid.NewGuid();

                //增加一个部门
                context.Department.Add(
                   new Department
                   {
                       ID = departmentID,
                       Name = "系统辅助部门",
                       ParentID = Guid.Empty
                   }
                );

                //增加一个超级管理员用户
                context.User.Add(
                     new User
                     {
                         ID = AdminID,
                         UserName = "admin",
                         Password = "123", //暂不进行加密
                         Name = "超级管理员",
                         DepartmentID = departmentID,
                     }
                );

                //增加一个系统管理菜单
                context.Menu.Add(
                    new Menu
                    {
                        ID = menuID,
                        Name = "系统管理",
                        Code = "System",
                        SerialNumber = 0,
                        ParentID = Guid.Empty,
                        Icon = "fa fa-folder",
                        Url="",
                        Type = 0,
                    }
                 );

                //增加四个基本功能操作菜单
                context.Menu.AddRange(
                   new Menu
                   {
                       ID = Guid.NewGuid(),
                       Name = "组织机构管理",
                       Code = "Department",
                       SerialNumber = 0,
                       ParentID = menuID,
                       Icon = "fa fa-edit",
                       Url = "",
                       Type = 1,
                   },
                   new Menu
                   {
                       ID = Guid.NewGuid(),
                       Name = "用户管理",
                       Code = "User",
                       SerialNumber = 1,
                       ParentID = menuID,
                       Icon = "fa fa-edit",
                       Url = "",
                       Type = 1,
                   },
                    new Menu
                    {
                        ID = Guid.NewGuid(),
                        Name = "角色管理",
                        Code = "Role",
                        SerialNumber = 2,
                        ParentID = menuID,
                        Icon = "fa fa-edit",
                        Url = "",
                        Type = 1,
                    },
                   new Menu
                   {
                       ID = Guid.NewGuid(),
                       Name = "菜单管理",
                       Code = "Menu",
                       SerialNumber = 3,
                       ParentID = menuID,
                       Icon = "fa fa-edit",
                       Url = "",
                       Type = 1,
                   }
                );
                context.SaveChanges();
            }
        }
    }
}
